/**
 * Created by xie on 2018/4/4.
 */
angular.module('module.auth')
    .controller('ResetpwdController', ResetpwdController);

ResetpwdController.$inject = ['$scope','$state','$cookieStore','$rootScope','AutherService'];

// 登录
function ResetpwdController($scope, $state,$cookieStore, $rootScope,AutherService) {

    var vm = this;
    vm.btnMessage="发送验证码";
    vm.sentMessage=sentMessage;
    vm.btnclass="btnclass";
    vm.updatePwd=updatePwd;
    vm.user = {
        "userid": null,
        "usertype": 1,
        "authcode": null,
        "password": "",
        "pwd":""
    }
    function sentMessage(){
        vm.btnMessage="已发送验证码";
        vm.btnclass="btnclasschange";
        AutherService.sentMessage(vm.user.userid).$promise.then(function (data) {
            response = data;
            console.log(response);
        }, function (err) {
            console.log(err);
            return alert(err.data.msg);
        });
    }


    function updatePwd() {
        if (!vm.user.userid) {
            vm.user = null;
            return alert("请输入手机号");
        }
        if (!vm.user.authcode) {
            vm.user = null;
            return alert("请输入验证码");
        }
        if(vm.user.password.length<6)
        {
            vm.userinfo = null;
            return alert("密码需大于6位");
        }
        if (!vm.user.password&&!vm.user.pwd) {
            vm.userinfo = null;
            return alert("密码不能为空");

        }
        if (vm.user.password != vm.user.pwd) {
            vm.user = null;
            return alert("两次输入的密码不一致");
        }


        console.log("user",vm.user);
        AutherService.updatePwd(vm.user).$promise.then(function (data) {
            response = data;
            console.log(response)
            alert("修改密码成功");
            $state.go('login');
        }, function (err) {
            console.log(err);
            return alert(err.data.msg);
        });

    }

}
